package com.cc.chioceFileListView;

import java.io.File;
import java.util.ArrayList;

import com.ygo.feihua.R;

import android.content.Context;
import android.graphics.Bitmap;
import android.graphics.Color;
import android.view.Gravity;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

public class ListViewAdapter extends BaseAdapter {

	ArrayList<String> filePathArrayList;

    public Bitmap folderBitmap, fileBitmap;
	
    Context context;
    
    int imageViewWidth;
    
    public ListViewAdapter(Context context, ArrayList<String> filePathArrayList, int imageViewWidth, Bitmap folderBitmap, Bitmap fileBitmap){
        this.filePathArrayList = filePathArrayList;
        this.context = context;
        this.imageViewWidth = imageViewWidth;
        this.folderBitmap = folderBitmap;
        this.fileBitmap =fileBitmap;
    }
    
    public void recycle(){
    	if(folderBitmap != null){
    		folderBitmap.recycle();
    	}
    	if(fileBitmap != null){
    		fileBitmap.recycle();
    	}
    }
    
	@Override
	public int getCount() {
		// TODO Auto-generated method stub
		return filePathArrayList.size();
	}

	@Override
	public Object getItem(int position) {
		// TODO Auto-generated method stub
		return null;
	}

	@Override
	public long getItemId(int position) {
		// TODO Auto-generated method stub
		return 0;
	}

	@Override
	public View getView(int position, View convertView, ViewGroup parent) {
		// TODO Auto-generated method stub
		ViewHolder viewHolder;
        if(convertView == null){
            viewHolder = new ViewHolder();
            LinearLayout linearLayout = new LinearLayout(context);
            linearLayout.setGravity(Gravity.CENTER_VERTICAL);
            linearLayout.setOrientation(LinearLayout.HORIZONTAL);
            linearLayout.setBackgroundColor(0);

            TextView textView = new TextView(context);
            textView.setTextSize(20);
            textView.setTextColor(Color.BLACK);

            ImageView imageView = new ImageView(context);
            imageView.setLayoutParams(new LinearLayout.LayoutParams(imageViewWidth, imageViewWidth));

            linearLayout.addView(imageView);
            linearLayout.addView(textView);
            convertView = linearLayout;
            viewHolder.imageView = imageView;
            viewHolder.textView = textView;
            convertView.setTag(viewHolder);
        }else{
            viewHolder = (ViewHolder)convertView.getTag();
        }

        /*if(filePathArrayList.get(position).isDirectory()){
            if(folderBitmap != null || !folderBitmap.isRecycled()){
            	viewHolder.imageView.setImageBitmap(folderBitmap);
            }
        }else{
            if(fileBitmap != null || !fileBitmap.isRecycled()){
            	viewHolder.imageView.setImageBitmap(fileBitmap);
            }
        }*/
        
        String path = filePathArrayList.get(position);
        if (!new File(path).isDirectory()){
			String fromt = "";		
			File file = new File(path);
			String fileName = file.getName();
			fromt = fileName.substring(fileName.lastIndexOf(".")+1,fileName.length());
			
			switch(fromt){
				case "txt":
					viewHolder.imageView.setImageDrawable(context.getResources().getDrawable(R.drawable.wx_txt));
					break;
				case "zip":
					viewHolder.imageView.setImageDrawable(context.getResources().getDrawable(R.drawable.wx_zip));
					break;
				case "conf":
					viewHolder.imageView.setImageDrawable(context.getResources().getDrawable(R.drawable.wx_kb));
					break;
				default:
					viewHolder.imageView.setImageDrawable(context.getResources().getDrawable(R.drawable.wx_wj));
					break;
			}
		}else{
			viewHolder.imageView.setImageDrawable(context.getResources().getDrawable(R.drawable.wx_wjj));
		}

        viewHolder.textView.setText(new File(filePathArrayList.get(position)).getName());

        return convertView;
	}

}
